<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="分类" prop="bannerType">
      <el-select v-model="dataForm.bannerType" placeholder="分类" :disabled="this.dataForm.id" @change="addBannerType()">
          <!-- <el-option
                v-for="item in dataForm.bannerTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
          </el-option> -->
          <el-option label="首页Banner" value=0 ></el-option>
          <el-option label="首页今日必买" value=1 ></el-option>
          <el-option label="首页新品上架" value=2 ></el-option>
          <el-option label="超级大玩家页Banner" value=3 ></el-option>
          <!-- <el-option label="首页分类活动图" value=4 ></el-option> -->
          <el-option label="社区团购" value="7"></el-option>
          <el-option label="邀请背景图" value="8"></el-option>
          <el-option label="启动页" value="11"></el-option>
          <el-option label="广告页" value="12"></el-option>
          <el-option label="引导页" value="13"></el-option>
          <el-option label="商品缺省图" value="14"></el-option>
          <el-option label="购物车缺省图" value="15"></el-option>
          <el-option label="精品专题" value="18"></el-option>
          <el-option label="品牌墙" value="19"></el-option>
          <el-option label="首页特卖" value="22"></el-option>
          <el-option label="首页精选" value="23"></el-option>
          <el-option label="个人中心轮播图" value="24"></el-option>
				</el-select>
    </el-form-item>
    <el-form-item label="背景色" prop="bannerColourValue" v-if="this.dataForm.bannerType === '0' || this.dataForm.bannerType === '22'">
      <el-color-picker v-model="dataForm.bannerColourValue"></el-color-picker>
    </el-form-item>
    <el-form-item label="渐变顶部" prop="bannerColourValue2" v-if="this.dataForm.bannerType === '22'">
      <el-color-picker v-model="dataForm.bannerColourValue2"></el-color-picker>
    </el-form-item>
    <el-form-item label="渐变底部" prop="bannerColourValue3" v-if="this.dataForm.bannerType === '22'">
      <el-color-picker v-model="dataForm.bannerColourValue3"></el-color-picker>
    </el-form-item>
    <el-form-item label="图片名称" prop="img">
      <!-- <el-input v-model="dataForm.imgName" placeholder="图片名称"></el-input> -->
      <el-upload
        :class="{hide:hideUpload}"
        :action="this.$http.adornUrl(`/banner/uploadBanner?token=${this.$cookie.get('token')}`)"
        :limit="1"
        list-type="picture-card"
        :file-list="dataForm.img"
        :on-preview="handlePreview"
        :on-success="handleSuccess"
        :on-remove="handleRemove"
        :on-exceed="onExceed">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-form-item>
    <!-- <el-form-item label="图片路径" prop="imgPath">
      <el-input v-model="dataForm.imgPath" placeholder="图片路径"></el-input>
    </el-form-item> -->
    <el-form-item label="排序" prop="seq">
      <el-input type="number" style="width: 100px" v-model="dataForm.seq" placeholder="排序，越大越靠前"></el-input>
    </el-form-item>
    <el-form-item label="跳转类型" prop="jumpType">
      <el-row  style="display: flex;">
        <el-select v-model="dataForm.jumpType" placeholder="分类" value="" style="margin-right: 30px">
          <el-option label="商品分类" :value="0" ></el-option>
          <el-option label="联盟店分类" :value="1" ></el-option>
          <el-option label="活动" :value="2" ></el-option>
          <el-option label="外链" :value="3" ></el-option>
          <el-option label="店铺" :value="4" ></el-option>
          <el-option label="专题" :value="5" ></el-option>
          <el-option label="无跳转" :value="6" ></el-option>
          <el-option label="商品" :value="7" ></el-option>
          <el-option label="品牌" :value="8" ></el-option>
          <el-option label="采购" :value="18" ></el-option>
          <!-- <el-cascader v-model="dataForm.jumpType" :options="outerClassList" :props="props" expand-trigger="hover" filterable></el-cascader> -->
        </el-select>
        <el-input v-model="dataForm.url" placeholder="请输入跳转类型链接"></el-input>
      </el-row>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input type="textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        limitCount: 1,
        hideUpload: false,
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        isAdd: true,
        outerClassList: [],
        props: {
          value: 'id',
          label: 'className',
          children: 'subGoodsClassList'
        },
        dataForm: {
          id: 0,
          bannerType: '',
          imgName: '',
          imgPath: '',
          img: [],
          jumpType: '',
          seq: '',
          url: '',
          remark: '',
          bannerColourValue: null,
          bannerColourValue2: null,
          bannerColourValue3: null,
          bannerTypeOptions: [{
            value: 0,
            label: '首页Banner'
          }, {
            value: 1,
            label: '首页今日必买'
          }, {
            value: 2,
            label: '首页今日抢购'
          }, {
            value: 3,
            label: '超级大玩家页Banner'
          }, {
            value: 4,
            label: '首页分类活动图'
          }]
        },
        dataRule: {
          bannerType: [
            { required: true, message: '分类不能为空', trigger: 'blur' }
          ],
          imgName: [
            { required: true, message: '图片名称不能为空', trigger: 'blur' }
          ],
          imgPath: [
            { required: true, message: '图片路径不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 图片预览
      handlePreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 文件上传成功的钩子函数
      handleSuccess (res, file, fileList) {
        this.hideUpload = fileList.length >= this.limitCount
        this.$message({
          type: 'info',
          message: '图片上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.img.push({name: 'tu', url: res.data})
        }
        // console.log(this.dataForm.img)
      },
      // 删除文件之前的钩子函数
      handleRemove (file, fileList) {
        this.hideUpload = fileList.length >= this.limitCount
        // debugger
        for (var i = 0; i < this.dataForm.img.length; i++) {
          var picc = this.dataForm.img[i]
          if (picc.url === file.url) {
            this.dataForm.img.splice(i, 1)
          }
        }
        this.$message({
          type: 'info',
          message: '已删除原有图片',
          duration: 6000
        })
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传1个图片',
          duration: 6000
        })
      },
      init (id) {
        this.hideUpload = false
        this.$http({
          url: this.$http.adornUrl(`/banner/queryOuterClassList`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            debugger
            this.outerClassList = data.data
          }
        })
        this.dataForm.bannerColourValue = null
        this.dataForm.url = ''
        this.dataForm.remark = ''
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/banner/getBannerById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bannerType = data.data.bannerType
                var pic = data.data.imgPath + '/' + data.data.imgName
                this.dataForm.img.push({name: 'tu', url: 'http://image.yilianbaihui.cn/consumer/' + pic})
                this.dataForm.jumpType = data.data.jumpType
                this.dataForm.url = data.data.url
                this.dataForm.seq = data.data.seq
                this.dataForm.remark = data.data.remark
                // this.dataForm.bannerColourValue = data.data.bannerColourValue
                if (data.data.bannerColourValue != null && data.data.bannerColourValue !== '') {
                  let color = data.data.bannerColourValue.split(',')
                  debugger
                  console.log(color)
                  for (let i = 0; i < color.length; i++) {
                    if (i == 0) {
                      this.dataForm.bannerColourValue = color[i]
                    } else if (i == 1) {
                      this.dataForm.bannerColourValue2 = color[i]
                    } else if (i == 2) {
                      this.dataForm.bannerColourValue3 = color[i]
                    }
                  }
                }
              }
              this.hideUpload = true
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (!this.isAdd) {
          this.$message.error('当前分类已超出数量限制！')
          return
        }
        if (this.dataForm.bannerType === '0' || this.dataForm.bannerType === '22') {
          if (this.dataForm.bannerColourValue == null || this.dataForm.bannerColourValue === '') {
            this.$message.error('请选择背景色')
            return
          }
        }
        if (this.dataForm.bannerType === '22') {
          if (this.dataForm.bannerColourValue2 === null || this.dataForm.bannerColourValue2 === '') {
            this.$message.error('渐变顶部')
            return
          }
          if (this.dataForm.bannerColourValue2 === null || this.dataForm.bannerColourValue2 === '') {
            this.$message.error('渐变底部')
            return
          }
        }
        let col = ''
        if (this.dataForm.bannerType === '22') {
          col = this.dataForm.bannerColourValue + ',' + this.dataForm.bannerColourValue2 + ',' + this.dataForm.bannerColourValue3
        } else if (this.dataForm.bannerType === '0') {
          col = this.dataForm.bannerColourValue
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/banner/${!this.dataForm.id ? 'addBanner' : 'modifyBannerById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'bannerType': this.dataForm.bannerType,
                'imgName': this.dataForm.img[0].url.substring(this.dataForm.img[0].url.lastIndexOf('/') + 1, this.dataForm.img[0].url.length),
                'imgPath': 'banner',
                'jumpType': this.dataForm.jumpType,
                'url': this.dataForm.url,
                'seq': this.dataForm.seq,
                'remark': this.dataForm.remark,
                'bannerColourValue': col
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      addBannerType () {
        // 如果类型是18，19，22
        if (this.dataForm.bannerType != null && this.dataForm.bannerType !== '') {
          if (this.dataForm.bannerType === '18' || this.dataForm.bannerType === '19' || this.dataForm.bannerType === '22') {
            this.$http({
              url: this.$http.adornUrl(`/banner/queryBannerList`),
              method: 'get',
              params: this.$http.adornParams({
                'bannerType': this.dataForm.bannerType,
                'page': 1,
                'limit': 10,
                })
            }).then(({data}) => {
              if (data && data.code === 0) {
                if (data.data.totalCount >= 1) {
                  this.$message.error('当前分类最多只能存在一个！')
                  this.isAdd = false
                }
              }
            })
          }
        } else if (this.dataForm.bannerType === '23') {
          this.$http({
            url: this.$http.adornUrl(`/banner/queryBannerList`),
            method: 'get',
            params: this.$http.adornParams({
              'bannerType': this.dataForm.bannerType,
              'page': 1,
              'limit': 10,
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              if (data.data.totalCount >= 9) {
                this.$message.error('当前分类最多只能存在9个！')
                this.isAdd = false
              }
            }
          })
        } else {
          this.isAdd = true
        }
      }
    }
  }
</script>
<style>
  .hide .el-upload--picture-card {  display: none; }
</style>
